<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>weakMap</title>
</head>

<body>
  <script>
    //ES6语法 利用weakMap避免内存泄露
    const oBtn1 = document.querySelector('#btn1');
    const oBtn2 = document.querySelector('#btn2');

    const oBtnMap = new WeakMap();

    //弱引用
    oBtnMap.set(oBtn1, handleBtn1Click);
    oBtnMap.set(oBtn2, handleBtn2Click);


    oBtn1.addEventListener('click', oBtnMap.get(oBtn1), false);
    oBtn2.addEventListener('click', oBtnMap.get(oBtn2), false);

    function handleBtn1Click() {}

    function handleBtn2Click() {}

    oBtn1.remove();
    oBtn2.remove();
  </script>
</body>

</html>